﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using HYView;

namespace HangerSupport
{
	public class HSCount
	{
		public HSCount(UIApplication uiApp)
		{
			this.m_uiApp = uiApp;
			this.m_doc = this.m_uiApp.ActiveUIDocument.Document;
			this.m_selection = this.m_uiApp.ActiveUIDocument.Selection;
			this.m_listTolHS = new List<Element>();
			this.m_listSelHS = new List<Element>();
			this.m_listResult = new List<HSCalResult>();
			this.GetTolHS();
		}

		public void GetTolHS()
		{
			this.m_listTolHS.Clear();
			IList<Element> list = HSDraw.GetElements(this.m_doc).ToElements();
			foreach (Element element in list)
			{
				bool flag = HSFilter.IsHSElement(element, "");
				if (flag)
				{
					this.m_listTolHS.Add(element);
				}
			}
			this.CountHSInfo(this.m_listTolHS);
		}

		public void SelectHS()
		{
			PipeSelect pipeSelect = new PipeSelect(this.m_uiApp);
			this.m_listSelHS = pipeSelect.SelectMultiHS("选择统计支吊架");
			this.CountHSInfo(this.m_listSelHS);
		}

		public void CountHSInfo(List<Element> listSelHS)
		{
			this.m_listResult.Clear();
			foreach (Element eleHS in listSelHS)
			{
				HSCalResult item = new HSCalResult(eleHS, this.m_uiApp);
				this.m_listResult.Add(item);
			}
		}

		public void CountHS()
		{
			DlgHSCount.ViewType viewType = DlgHSCount.ViewType.eViewTypeMat;
			DlgHSCount dlgHSCount = new DlgHSCount(this.m_uiApp, this.m_listResult, viewType);
			bool flag = DialogResult.OK != dlgHSCount.ShowDialog();
			if (!flag)
			{
				for (;;)
				{
					viewType = dlgHSCount.m_kViewType;
					switch (dlgHSCount.m_kCountType)
					{
					case DlgHSCount.CountTYPE.eCountAll:
					{
						this.CountHSInfo(this.m_listTolHS);
						dlgHSCount = new DlgHSCount(this.m_uiApp, this.m_listResult, viewType);
						bool flag2 = DialogResult.OK != dlgHSCount.ShowDialog();
						if (flag2)
						{
							goto Block_3;
						}
						break;
					}
					case DlgHSCount.CountTYPE.eCountSelect:
					{
						this.SelectHS();
						dlgHSCount = new DlgHSCount(this.m_uiApp, this.m_listResult, viewType);
						bool flag3 = DialogResult.OK != dlgHSCount.ShowDialog();
						if (flag3)
						{
							goto Block_4;
						}
						break;
					}
					case DlgHSCount.CountTYPE.eCountDraw:
					{
						CmdViewType types = CmdViewType.FloorPlan;
						bool flag4 = SwitchView.SetUsableView(types, this.m_uiApp) > 0;
						if (flag4)
						{
							goto Block_5;
						}
						XYZ ptBase = XYZ.Zero;
						try
						{
							XYZ pt = this.m_selection.PickPoint("请选择布置表格的左上角的位置");
							ptBase = Utils.PtTo2d(pt);
						}
						catch (Exception ex)
						{
							ptBase = null;
							return;
						}
						dlgHSCount.DrawTable(ptBase);
						dlgHSCount = new DlgHSCount(this.m_uiApp, this.m_listResult, viewType);
						bool flag5 = DialogResult.OK != dlgHSCount.ShowDialog();
						if (flag5)
						{
							goto Block_7;
						}
						break;
					}
					}
				}
				Block_3:
				Block_4:
				Block_5:
				Block_7:;
			}
		}

		private UIApplication m_uiApp;

		private Document m_doc;

		private Selection m_selection;

		private List<Element> m_listTolHS;

		private List<Element> m_listSelHS;

		private List<HSCalResult> m_listResult;
	}
}
